//
//  DDModifyPwdViewController.m
//  Dingdong
//
//  Created by 程强 on 16/11/11.
//  Copyright © 2016年 程强. All rights reserved.
//

#import "DDModifyPwdViewController.h"
#import "DDUserManager.h"

@interface DDModifyPwdViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *oldTxf;
@property (weak, nonatomic) IBOutlet UITextField *nTxf;
@property (weak, nonatomic) IBOutlet UITextField *confirmTxf;
@property (weak, nonatomic) IBOutlet UIButton *confirmBtn;
@property (weak, nonatomic) IBOutlet UIImageView *oldIcon;
@property (weak, nonatomic) IBOutlet UIImageView *nIcon;
@property (weak, nonatomic) IBOutlet UIImageView *confirmIcon;
@property (strong, nonatomic) DDUserManager *manager;
@end

@implementation DDModifyPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.manager = [DDUserManager new];
    [self layoutModifyView];
}
- (void)layoutModifyView
{
    self.confirmBtn.layer.cornerRadius = 3.0;
    [self.oldTxf becomeFirstResponder];
}
- (BOOL)checkInput
{
    if (self.oldTxf.text.length == 0) {
        [self showMessageHUD:@"Please enter old password" delayHide:1.0];
        return NO;
    }
    if (![NOValidateHelper validatePassword:self.nTxf.text]) {
        [self showMessageHUD:@"Please enter a new valid password" delayHide:1.0];
        return NO;
    }
    if (![self.confirmTxf.text isEqualToString:self.nTxf.text]) {
        [self showMessageHUD:@"Please confirm password" delayHide:1.0];
        return NO;
    }
    return YES;
}
- (IBAction)confirmClick:(id)sender
{
    if (![self checkInput]) {
        return;
    }
    weak_Self(self);
    [[self.manager modifyPassword:self.oldTxf.text nPwd:self.nTxf.text]subscribeNext:^(id x) {
        [weakSelf showMessageHUD:@"Modify Success" delayHide:1.0];
        [weakSelf gotoBack];
    } error:^(NSError *error) {
        
    } completed:^{
        
    }];
}

- (void)resignKeyboard
{
    [self.oldTxf resignFirstResponder];
    [self.nTxf resignFirstResponder];
    [self.confirmTxf resignFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self resignKeyboard];
    return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField == self.oldTxf) {
        self.oldIcon.highlighted = YES;
        self.nIcon.highlighted = NO;
        self.confirmIcon.highlighted = NO;
    }else if (textField == self.nTxf){
        self.nIcon.highlighted = YES;
        self.oldIcon.highlighted = NO;
        self.confirmIcon.highlighted = NO;
    }else{
        self.confirmIcon.highlighted = YES;
        self.oldIcon.highlighted = NO;
        self.nIcon.highlighted = NO;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
